package com.praveen.reminder;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ReminderAdapter extends BaseExpandableListAdapter {
	private Context mContext;
	private ArrayList<ArrayList<ReminderData>> mObj;
	private int mGroupResource;
	private int mRowResource;
	private ArrayList<String> mGroups;
	
	public ReminderAdapter(Context context, int groupResourceId, int rowResourceId, ArrayList<String> groups, ArrayList<ArrayList<ReminderData>> objects) {
		mContext = context;
		mGroupResource = groupResourceId;
		mRowResource = rowResourceId;
		mGroups = groups;
		mObj = objects;
	}
	
	public Object getChild(int groupPosition, int childPosition) {
		return mObj.get(groupPosition).get(childPosition);
	}
	
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}
	
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		ReminderData data = (ReminderData)getChild(groupPosition, childPosition);
		LinearLayout rView;
		if (convertView == null) {
			rView = new LinearLayout(mContext);
			LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(mRowResource, rView, true);
		}
		else {
			rView = (LinearLayout)convertView;
		}
		TextView d = (TextView)rView.findViewById(R.id.dateEntry);
		TextView t = (TextView)rView.findViewById(R.id.timeEntry);
		TextView r = (TextView)rView.findViewById(R.id.textEntry);
		d.setText(Utility.pad(data.getmDay()) + "/" + Utility.pad(data.getmMonth()) + "/" + Utility.pad(data.getmYear()));
		t.setText(Utility.pad(data.getmHour()) + ":" + Utility.pad(data.getmMinute()));
		r.setText(data.getmReminderText());
		return rView;
	}
	
	public int getChildrenCount(int groupPosition) {
		return mObj.get(groupPosition).size();
	}
	
	public Object getGroup(int groupPosition) {
		return mGroups.get(groupPosition);
	}
	
	public int getGroupCount() {
		return mGroups.size();
	}
	
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}
	
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		String groupName = (String)getGroup(groupPosition);
		LinearLayout rView;
		if (convertView == null) {
			rView = new LinearLayout(mContext);
			LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(mGroupResource, rView, true);
		}
		else {
			rView = (LinearLayout)convertView;
		}
		TextView tv = (TextView)rView.findViewById(R.id.groupheader);
		tv.setText(groupName);
		return rView;
	}
	
	public boolean hasStableIds() {
		return true;
	}
	
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
